import YAML from 'yaml'
import chokidar from 'chokidar'
import fs from 'node:fs'
import YamlReader from '../model/YamlReader.js'
import cfg from '../../../lib/config/config.js'
import path from 'path'
const Path = process.cwd();
const Plugin_Name = 'js-box-plugin'
const Plugin_Path = path.join(Path, 'plugins', Plugin_Name)
const cfg_path = path.join(Plugin_Path, 'config', 'setting')
class Config {
  constructor() {
    this.config = {}
    /** 监听文件 */
    this.watcher = { config: {}, defSet: {} }
    this.initCfg()
  }
  /** 初始化配置 */
  initCfg() {
    fs.mkdirSync(cfg_path, { recursive: true });
    let path = `${Plugin_Path}/config/setting/user/`
    let pathDef = `${Plugin_Path}/config/setting/default/`
    const files = fs.readdirSync(pathDef).filter(file => file.endsWith('.yaml'))
    for (let file of files) {
      if (!fs.existsSync(`${path}${file}`)) {
        fs.copyFileSync(`${pathDef}${file}`, `${path}${file}`)
      }
    }
  }

  /**主人QQ */
  get masterQQ() {
    return cfg.masterQQ
  }

  /**沙盒设置 */
  get admin_set() {
    return this.getset("admin_set")
  }

   /** 终端*/
get command() {
    return this.getset("command")
}

  /**戳一戳*/
get chuo() {
    return this.getset("chuo")
}

  /**戏天*/
get manager() {
    return this.getset("manager")
}

  /**我的新名字*/
get callme() {
    return this.getset("callme")
}

  /**伪造消息*/
get remor() {
    return this.getset("remor")
}

  /**上传面板图*/
get panel() {
    return this.getset("panel")
}

  /**谁艾特我*/
get whoatme() {
    return this.getset("whoatme")
}

   /** 设置 */
  getset(name) {
    let config = this.getConfig(name)
    let def = this.getdefSet(name)
    return { ...def, ...config }
   }
   
  /** 默认配置 */
  getdefSet(name) {
    return this.getYaml('default', name)
  }

  /** 用户配置 */
  getConfig(name) {
    return this.getYaml('user', name)
  }

  /**
   * 获取配置yaml
   * @param type 默认跑配置-defSet，用户配置-config
   * @param name 名称
   */
  getYaml(type, name) {
    let file = `${Plugin_Path}/config/setting/${type}/${name}.yaml`
    let key = `${type}.${name}`

    if (this.config[key]) return this.config[key]

    this.config[key] = YAML.parse(
      fs.readFileSync(file, 'utf8')
    )

    this.watch(file, name, type)

    return this.config[key]
  }

  /** 监听配置文件 */
  watch(file, name, type = 'default') {
    let key = `${type}.${name}`

    if (this.watcher[key]) return

    const watcher = chokidar.watch(file)
    watcher.on('change', path => {
      delete this.config[key]
      if (typeof Bot == 'undefined') return
      logger.mark(`[沙盒修改配置文件][${type}][${name}]`)
      if (this[`change_${name}`]) {
        this[`change_${name}`]()
      }
    })

    this.watcher[key] = watcher
  }

  /**
   * @description: 修改设置
   * @param {String} name 文件名
   * @param {String} key 修改的key值
   * @param {*} value 修改的value值
   * @return {Boolean} 返回是否成功写入
   */
  modify(name, key, value) {
    let path = `${Plugin_Path}/config/setting/user/${name}.yaml`
    new YamlReader(path).set(key, value)
    delete this.config[`config.${name}`]
  }

}
export default new Config()